package com.ios.service.impl;

import com.ios.dao.PsychologicalTestMapper;
import com.ios.dao.PsychologicalTestQuestionMapper;
import com.ios.dao.PsychologicalTestResultMapper;
import com.ios.dao.PsychologicalTestTypeMapper;
import com.ios.dto.PsychologicalQuestionExecution;
import com.ios.dto.PsychologicalTestExecution;
import com.ios.dto.PsychologicalTestTypeExecution;
import com.ios.dto.PsychologyResultExecution;
import com.ios.entity.PsychologicalTest;
import com.ios.entity.PsychologicalTestQuestion;
import com.ios.entity.PsychologicalTestResult;
import com.ios.entity.PsychologicalTestType;
import com.ios.enums.PsychologicalTestEnum;
import com.ios.service.IPsychologicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class PsychologicalServiceImpl implements IPsychologicalService{
    @Autowired
    private PsychologicalTestTypeMapper psychologicalTestTypeMapper;
    @Autowired
    private PsychologicalTestMapper psychologicalTestMapper;
    @Autowired
    private PsychologicalTestQuestionMapper psychologicalTestQuestionMapper;
    @Autowired
    private PsychologicalTestResultMapper psychologicalTestResultMapper;


    @Override
    public PsychologicalTestTypeExecution ShowAllType() {
        try {
            List<PsychologicalTestType> list = psychologicalTestTypeMapper.ShowAllType();
            return new PsychologicalTestTypeExecution(PsychologicalTestEnum.SUCCESS_SHOWTYPE,list);
        }catch (Exception e){
            return new PsychologicalTestTypeExecution(PsychologicalTestEnum.FAIL_SHOWTYPE);
        }
    }

    @Override
    public PsychologicalTestExecution ShowTestByType(String typename) {
        if (typename.equals(null)){
            /*检查参数是否为空*/
            return new PsychologicalTestExecution(PsychologicalTestEnum.NULL_PARAM);
        }else {
            try {
                List<PsychologicalTest> list =
                        psychologicalTestMapper.showByType(psychologicalTestTypeMapper.SelectIdByName(typename));
                if (list.isEmpty()){
                /*若该类别下没有测试*/
                    return new PsychologicalTestExecution(PsychologicalTestEnum.NULL_TEST);
                }else {
                    return new PsychologicalTestExecution(PsychologicalTestEnum.SUCCESS_SHOWTEST,list);
                }
            }catch (Exception e) {
                return new PsychologicalTestExecution(PsychologicalTestEnum.FAIL_SHOWTEST);
            }
        }
    }

    @Override
    public PsychologicalTestExecution ShowAllTest() {
        try {
            List<PsychologicalTest> list = psychologicalTestMapper.showAll();
            return new PsychologicalTestExecution(PsychologicalTestEnum.SUCCESS_SHOWTEST,list);
        }catch (Exception e){
            return new PsychologicalTestExecution(PsychologicalTestEnum.FAIL_SHOWTEST);
        }
    }

    @Override
    public PsychologicalQuestionExecution ShowQuestion(int test_id) {
        if (test_id == -1){
            return new PsychologicalQuestionExecution(PsychologicalTestEnum.NULL_PARAM);
        }else if (!CheckTest(test_id)){
            return new PsychologicalQuestionExecution(PsychologicalTestEnum.NULL_TEST);
        }else {
            try {
                List<PsychologicalTestQuestion> questions = psychologicalTestQuestionMapper.selectByTest(test_id);
                return new PsychologicalQuestionExecution(PsychologicalTestEnum.SUCCESS_SHOWQUESTION,questions);
            }catch (Exception e){
                return new PsychologicalQuestionExecution(PsychologicalTestEnum.FAIL_SHOWQUESTION);
            }
        }
    }

    @Override
    public PsychologyResultExecution TestResult(String answer, int test_id) {
        /*判断参数是否为空*/
        if (answer.equals(null)||test_id==-1){
            return new PsychologyResultExecution(PsychologicalTestEnum.NULL_PARAM);
        }if (!CheckTest(test_id)){
            return new PsychologyResultExecution(PsychologicalTestEnum.NULL_TEST);
        }else {
            try {
                float score = 0;/*统计分数*/
                /*1：获取测试对应的评分规则和题目相关信息*/
                PsychologicalTestResult result=psychologicalTestResultMapper.selectByTest(test_id);
                List<PsychologicalTestQuestion> Qlist = psychologicalTestQuestionMapper.selectByTest(test_id);
                /*2：判断测试是否完成*/
                if (answer.length()!=Qlist.size()){
                    return new PsychologyResultExecution(PsychologicalTestEnum.FAIL);
                }else {
                     /*3：解析结果字符串并统计分数*/
                    for (int i=0;i<answer.length();i++){
                        char a = answer.charAt(i);
                        PsychologicalTestQuestion pq = Qlist.get(i);
                        if (a=='A'){
                            score += pq.getScoreA();
                        }else if (a=='B'){
                            score += pq.getScoreB();
                        }else if (a=='C'){
                            score += pq.getScoreC();
                        }else if (a=='D'){
                            score += pq.getScoreD();
                        }
                    }
                /*4：根据分数生成结果*/
                    String resultInfo = new String();
                    if (score<result.getScoreA()){
                        resultInfo = result.getResultA();
                    }else if (score>=result.getScoreA()&&score<result.getScoreB()){
                        resultInfo = result.getResultB();
                    }else if (score>=result.getScoreB()&&score<result.getScoreC()){
                        resultInfo = result.getResultC();
                    }else if (score>=result.getScoreC()&&score<result.getScoreD()){
                        resultInfo = result.getResultD();
                    }else {
                        resultInfo = result.getResultE();
                    }
                    return new PsychologyResultExecution(PsychologicalTestEnum.SUCCESS_RESULT,result,score,resultInfo);
                }
            }catch (Exception e){
                return new PsychologyResultExecution(PsychologicalTestEnum.FAIL_RESULT);
            }
        }
    }

    /*检查测试是否存在*/
    private boolean CheckTest(int test_id){
        List<PsychologicalTest> list = psychologicalTestMapper.showAll();
        for (PsychologicalTest test:list){
            if (test.getId()==test_id){
                return true;
            }
        }
        return false;
    }

}
